package com.viknix.threadpool.manager.example.config;

import com.viknix.threadpool.manager.client.annotations.ThreadPoolManager;
import com.viknix.threadpool.manager.client.core.DynamicThreadPoolWrap;
import com.viknix.threadpool.manager.client.builder.ThreadPoolBuilder;
import com.viknix.threadpool.manager.client.enums.BlockingQueueEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: Dongqi
 * @Date: 2021/10/21 15:26
 * @Version 1.0
 * @Description: 客户端配置类
 */
@Slf4j
@Configuration
public class ThreadPoolManagerConfig {

    /**
     * 生产者 threadPoolId
     */
    public static final String MESSAGE_PRODUCE = "producer-thread-pool";
    /**
     * 生产者线程前缀
     */
    public static final String PRODUCE_THREAD_NAME_PREFIX = "produce-thread";
    /**
     * 消费者 threadPoolId
     */
    public static final String MESSAGE_CONSUME = "consumer-thread-pool";

    /**
     * 通过 DynamicThreadPoolWrap 的方式向服务端注册监听。
     * @return 动态线程池的包装对象
     */
    @Bean
    public DynamicThreadPoolWrap messageCenterConsumeThreadPool() {
        return new DynamicThreadPoolWrap(MESSAGE_CONSUME);
    }

    /**
     * 通过 @ThreadPoolManager 注解的方式向服务端注册监听，
     * 目前必须是 DynamicThreadPoolExecutor 动态线程池才能支持线程的监听。
     * @return DynamicThreadPoolExecutor 动态线程池
     */
    @Bean
    @ThreadPoolManager
    public ThreadPoolExecutor customThreadPoolExecutor() {
        // 根据默认参数构建一个新的线程池
        return ThreadPoolBuilder.builder()
                .setThreadNamePrefix(PRODUCE_THREAD_NAME_PREFIX)
                .setThreadPoolId(MESSAGE_PRODUCE)
                .setBlockingQueueCode(BlockingQueueEnum.LINKED_BLOCKING_QUEUE.getCode())
                .setDynamicThreadPool(true)
                .build();
    }
}
